package com.aiju.utils;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: 佐为
 * @date: 2016-11-23 18:55
 * @version: V1.0
 * @Description: 检查工具
 */
public class CheckUtil {

    /**
     * 大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数
     * 此方法中前三位格式有：
     * 13+任意数
     * 15+除4的任意数
     * 18+除1和4的任意数
     * 17+除9的任意数
     * 147
     */
    public static boolean checkPhone(String str) {
        if (TextUtils.isEmpty(str) || str.length() < 11) {
            return false;
        }

        String regExp = "^((13[0-9])|(15[^4])|(18[0,1,2,3,4,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }


    /**
     * 区号+座机号码+分机号码
     *
     * @param str
     * @return
     */
    public static boolean checkLandline(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        String regExp = "^$|(0[0-9]{2,3}\\-)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 检查用户名
     *
     * @param str
     * @return
     */
    public static boolean checkNickName(String str) {
        String regExp = "^[\\u4E00-\\u9FA5A-Za-z0-9_]{1,15}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 会员名
     *
     * @param str
     * @return
     */
    public static boolean checkMemberName(String str) {
        String regExp = "^[\\u4E00-\\u9FA5A-Za-z0-9_]{1,11}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 检查会员卡名称
     *
     * @param str
     * @return
     */
    public static boolean checkMemberCardName(String str) {
        String regExp = "^[\\u4E00-\\u9FA5A-Za-z0-9]{1,10}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 检查商品名称
     *
     * @param str
     * @return
     */
    public static boolean checkGoodName(String str) {
        String regExp = "^[\\u4E00-\\u9FA5A-Za-z0-9]{1,12}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 检查账本名称
     *
     * @param str
     * @return
     */
    public static boolean checkAccountName(String str) {
        String regExp = "^[\\u4E00-\\u9FA5A-Za-z0-9]{1,4}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 两位小数
     *
     * @param str
     * @return
     */
    public static boolean checkMember(String str) {
        Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }

    /**
     * 检查用户密码(8-20数字、字母、下划线)
     *
     * @param str
     * @return
     */
    public static boolean checkPassword(String str) {
        String regExp = "^\\w{8,20}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 检查验证码
     *
     * @param str
     * @return
     */
    public static boolean checkCode(String str) {
        String regExp = "^\\d{6}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 检查价格
     *
     * @param str
     * @return
     */
    public static boolean checkPrice(String str) {
        String regExp = "(^[1-9]\\d*(\\.\\d{1,2})?$)|(^0(\\.\\d{1,2})?$)";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 利用正则表达式判断字符串是否是数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 验证号码 手机号 固话均可
     */
    public static boolean isPhoneNumberValid(String phoneNumber) {
        boolean isValid = false;
        String expression = "((^(13|15|18)[0-9]{9}$)|(^0[1,2]{1}d{1}-?d{8}$)|"
                + "(^0[3-9] {1}d{2}-?d{7,8}$)|"
                + "(^0[1,2]{1}d{1}-?d{8}-(d{1,4})$)|"
                + "(^0[3-9]{1}d{2}-? d{7,8}-(d{1,4})$))";
        CharSequence inputStr = phoneNumber;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

    /**
     * 验证多个号码号码","分割 手机号 固话均可
     */
    public static boolean isMultiplePhone(String phoneNumber) {
        if (phoneNumber.contains(",")) {
            String split[] = phoneNumber.split(",");
            for (String phone : split) {
                if (TextUtils.isEmpty(phone)) {
                    T.ToastShort("门店电话不合规，请重新输入");
                    return false;
                }
                if (!checkAllPhone(phone)) {
                    T.ToastShort("门店电话不合规，请重新输入");
                    return false;
                }
            }
        } else {
            boolean all = checkAllPhone(phoneNumber);
            if (!all) {
                T.ToastShort("门店电话不合规，请重新输入");
            }
            return all;
        }
        return true;
    }

    /**
     * 验证手机号 座机号
     */
    public static boolean checkAllPhone(String phoneNumber) {
        if (TextUtils.isEmpty(phoneNumber)) {
            return false;
        }
        return checkPhone(phoneNumber) ? true : checkLandline(phoneNumber);
    }
}
